[3.13] gh-142831: Fix use-after-free in json encoder during re-entrant mutation (gh-142851)#150079
Open
ashm-dev wants to merge 1 commit into
Open
[3.13] gh-142831: Fix use-after-free in json encoder during re-entrant mutation (gh-142851)#150079ashm-dev wants to merge 1 commit into
ashm-dev wants to merge 1 commit into
Conversation
… mutation (pythongh-142851) User callbacks invoked during JSON encoding (e.g. the `default` callback or a custom string encoder) can mutate or clear the dict or sequence being encoded, invalidating borrowed references to items, keys, and values. Hold strong references unconditionally while iterating. Co-authored-by: Kumar Aditya <kumaraditya@python.org> Co-authored-by: Gregory P. Smith <greg@krypto.org> (cherry picked from commit 235fa72)
This was referenced May 19, 2026
Documentation build overview
454 files changed ·
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Manual backport of #142851 to 3.13 (the bot couldn't cleanly cherry-pick due to refactors in
main).The fix had to be adapted because:
_encoder_iterate_*_lock_heldhelpers introduced later inmain. Added thePy_INCREF/Py_DECREFaround each borrowed item/key/value inline inencoder_listencode_dict(both thePyMapping_ItemsandPyDict_Nextpaths) andencoder_listencode_list.encoder_encode_key_valuein 3.13 has a different signature (nodctparameter, different separator handling).Only the two UAF regression tests from the original PR were included —
test_current_indent_levelfrom the surrounding context is unrelated to this fix and would require additional 3.14-only changes.Built
--with-pydebugand ran./python -m test test_json -vlocally — all 197 tests pass.json.encodermapping iteration via re-entrant key encoder #142831